package com.im.service.message.service.impl;

import com.im.service.config.AppConfig;
import com.im.common.enums.*;
import com.im.common.enums.exception.FriendShipErrorCode;
import com.im.common.enums.exception.GroupErrorCode;
import com.im.common.enums.exception.MessageErrorCode;
import com.im.common.exception.BusinessException;
import com.im.service.friendship.entity.FriendShip;
import com.im.service.friendship.model.req.GetRelationReq;
import com.im.service.friendship.service.ImFriendService;
import com.im.service.group.entity.ImGroupEntity;
import com.im.service.group.model.resp.GetRoleInGroupResp;
import com.im.service.group.service.ImGroupMemberService;
import com.im.service.group.service.ImGroupService;
import com.im.service.message.service.CheckSendMessageService;
import com.im.service.user.entity.ImUserDataEntity;
import com.im.service.user.service.ImUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 校验消息服务类
 */
@Service
public class CheckSendMessageServiceImpl implements CheckSendMessageService {

    @Autowired
    private ImUserService imUserService;  // IM用户服务
    @Autowired
    private ImFriendService imFriendService;  // IM好友服务
    @Autowired
    private ImGroupService imGroupService;  // IM群组服务
    @Autowired
    private ImGroupMemberService imGroupMemberService;  // IM群成员服务
    @Autowired
    private AppConfig appConfig;  // 应用配置

    /**
     * 检查发送者是否被禁言或屏蔽
     *
     * @param fromId 发送者ID
     * @param appId  应用ID
     */
    public void checkSenderForvidAndMute(String fromId, Integer appId) {
        ImUserDataEntity singleUserInfo = imUserService.getSingleUserInfo(fromId, appId);
        if (singleUserInfo.getForbiddenFlag() == UserForbiddenFlagEnum.FORBIBBEN.getCode()) {
            throw new BusinessException(MessageErrorCode.FROMER_IS_FORBIBBEN);
        } else if (singleUserInfo.getSilentFlag() == UserSilentFlagEnum.MUTE.getCode()) {
            throw new BusinessException(MessageErrorCode.FROMER_IS_MUTE);
        }
    }

    /**
     * 检查好友关系
     *
     * @param fromId 发送者ID
     * @param toId   接收者ID
     * @param appId  应用ID
     */
    public void checkFriendShip(String fromId, String toId, Integer appId) {
        if (appConfig.isSendMessageCheckFriend()) {
            // 构建请求对象，查询发送者与接收者的好友关系
            GetRelationReq fromReq = new GetRelationReq();
            fromReq.setFromId(fromId);
            fromReq.setToId(toId);
            fromReq.setAppId(appId);
            FriendShip fromRelation = imFriendService.getRelation(fromReq);
            if (fromRelation == null) {
                throw new BusinessException("好友关系不存在");
            }
            // 构建请求对象，查询接收者与发送者的好友关系
            GetRelationReq toReq = new GetRelationReq();
            toReq.setFromId(toId);
            toReq.setToId(fromId);
            toReq.setAppId(appId);
            FriendShip toRelation = imFriendService.getRelation(toReq);
            if (toRelation == null) {
                throw new BusinessException("好友关系不存在");
            }
            // 检查好友关系状态
            if (FriendShipStatusEnum.FRIEND_STATUS_NORMAL.getCode() != fromRelation.getStatus()) {
                throw new BusinessException(FriendShipErrorCode.FRIEND_IS_DELETED);
            }
            if (FriendShipStatusEnum.FRIEND_STATUS_NORMAL.getCode() != toRelation.getStatus()) {
                throw new BusinessException(FriendShipErrorCode.FRIEND_IS_DELETED);
            }
            // 检查是否开启黑名单检查
            if (appConfig.isSendMessageCheckBlack()) {
                // 检查发送者是否在接收者的黑名单中
                if (FriendShipStatusEnum.BLACK_STATUS_NORMAL.getCode() != fromRelation.getBlack()) {
                    throw new BusinessException(FriendShipErrorCode.FRIEND_IS_BLACK);
                }
                // 检查接收者是否在发送者的黑名单中
                if (FriendShipStatusEnum.BLACK_STATUS_NORMAL.getCode() != toRelation.getBlack()) {
                    throw new BusinessException(FriendShipErrorCode.TARGET_IS_BLACK_YOU);
                }
            }
        }
    }


    /**
     * 检查群组消息
     *
     * @param fromId  发送者ID
     * @param groupId 群组ID
     * @param appId   应用ID
     */
    public void checkGroupMessage(String fromId, String groupId, Integer appId) {
        checkSenderForvidAndMute(fromId, appId);
        // 判断群组逻辑
        ImGroupEntity group = imGroupService.getGroup(groupId, appId);
        // 判断群成员是否在群内
        GetRoleInGroupResp roleInGroupOne = imGroupMemberService.getRoleInGroupOne(groupId, fromId, appId);
        // 判断群组是否被禁言
        // 如果被禁言，只有群管理员和群主可以发言
        if (group.getMute() == GroupMuteTypeEnum.MUTE.getCode() && (roleInGroupOne.getRole() == GroupMemberRoleEnum.MAMAGER.getCode() || roleInGroupOne.getRole() == GroupMemberRoleEnum.OWNER.getCode())) {
            throw new BusinessException(GroupErrorCode.THIS_GROUP_IS_MUTE);
        }
        if (roleInGroupOne.getSpeakDate() != null && roleInGroupOne.getSpeakDate() > System.currentTimeMillis()) {
            throw new BusinessException(GroupErrorCode.GROUP_MEMBER_IS_SPEAK);
        }
    }
}